package com.yanqu.road.server.manger.activity.platform;

import com.yanqu.road.dao.impl.player.UserAttributeDaoImpl;
import com.yanqu.road.dao.impl.servercenter.ServerInfoDaoImpl;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.poster.ActivityPosterInfo;
import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.player.UserAttribute;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.servercenter.PlatformActivityConfig;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.pb.activity.PlatformActivityProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.*;

public class PlatformActivityMgr extends TempMgr {

    private static List<PlatformActivityConfig> platformActivityConfigList;

    private static List<ActivityPosterInfo> posterInfoList;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        platformActivityConfigList = new ServerInfoDaoImpl().getPlatformActivityConfigList();
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivityData(){
        getLogger().info("reload PlatformActivityMgr Activity start");

        List<ActivityPosterInfo> tempPosterInfoList = new ArrayList<>();

        List<Integer> childTypes = Arrays.asList(68, 69, 72, 74); // 子活动类型 74 -- 返利活动海报
        List<ActivityInfo> openActivityInfoList =
                NormalActivityMgr.getInfosByChildTypes(eActivityType.PlatformActivity.getValue(), childTypes);

        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no PlatformMailAnti  post Activity in show time");
            tempPosterInfoList = null;
        } else {
            for (ActivityInfo tempActivityInfo : openActivityInfoList) {
                List<ActivityPosterInfo> activityPosterInfoList = new ServerInfoDaoImpl().getActivityPosterInfoList(tempActivityInfo.getActivityId());
                tempPosterInfoList.addAll(activityPosterInfoList);
            }
            getLogger().info("one PlatformMailAnti  post Activity in show time");
        }
        posterInfoList = tempPosterInfoList;

        syncPosterActivityConfig(null);

        getLogger().info("reload PlatformActivityMgr Activity end");
    }

    public static void syncPosterActivityConfig(GamePlayer player) {
        if(posterInfoList == null || posterInfoList.size() == 0){
            return;
        }
        PlatformActivityProto.PlatformConfigInfoSyncMsg.Builder sync = PlatformActivityProto.PlatformConfigInfoSyncMsg.newBuilder();
        for (ActivityPosterInfo posterInfo : posterInfoList) {
            PlatformActivityProto.ActivityPostInfoTemp.Builder builder = PlatformActivityProto.ActivityPostInfoTemp.newBuilder();
            builder.setId(posterInfo.getId());
            builder.setJump(posterInfo.getJump());
            builder.setPic(posterInfo.getPic());
            builder.setPos(posterInfo.getPos());
            builder.setType(posterInfo.getType());
            builder.setShowId(posterInfo.getShowId());
            builder.setBigType(posterInfo.getBigType());
            builder.setChildType(posterInfo.getChildType());
            builder.setFirstShow(posterInfo.getFirstShow());
            sync.addPost(builder);
        }
        if(player != null){
            player.sendPacket(Protocol.U_PLATFORM_ACTIVITY_CONFIG_SYNC, sync);
        }else {
            for (GamePlayer gamePlayer : GamePlayerMgr.getCopyPlayerList()) {
                gamePlayer.sendPacket(Protocol.U_PLATFORM_ACTIVITY_CONFIG_SYNC, sync);
            }
        }
    }

    public static boolean isSyncPlatformActivity(int bigActivityType, int childActivityType, String appId, int clientType, int phonePlatform) {
        if (platformActivityConfigList == null || platformActivityConfigList.size() == 0) {
            return true;
        }
        List<PlatformActivityConfig> pacs = platformActivityConfigList;
        for (PlatformActivityConfig config : pacs) {
            // 活动类型 appId h5/app类型 相同 判断登录的手机平台相同或者全屏蔽则都不下发
            if (config.getActivityBigType() == bigActivityType && config.getActivityChildType() == childActivityType
                    && appId.equals(config.getAppId()) && clientType == config.getClientType()) {
                if (config.getPhonePlatform() == 0 || config.getPhonePlatform() == phonePlatform) {
                    return false;
                }
            }
        }

        return true;
    }

    public List<PlatformActivityConfig> getPlatformActivityConfigList() {
        return platformActivityConfigList;
    }

    public void setPlatformActivityConfigList(List<PlatformActivityConfig> platformActivityConfigList) {
        this.platformActivityConfigList = platformActivityConfigList;
    }

    public static int getReward(GamePlayer player, int type) {
        SimpleActivityResult result = new SimpleActivityResult();
        result.check(type);
        if(result.ret != 0){
            return result.ret;
        }

        if (type == ePlayerAttrType.WanXinBuLuoReward.getValue()) {
            if(!SystemOpenMgr.systemOpen(player, eSystemId.WanXinBuLuoReward.getValue())){
                return GameErrorCode.E_SYSTEM_NO_OPEN;
            }
        }

        AttributeModule attributeModule = player.getModule(AttributeModule.class);
        long v = attributeModule.getAttribute(result.attrType);
        if(v == 0){
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        if(v == 2){
            return GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
        }
        String reward = result.activityInfo.getParam6();
        attributeModule.setAttribute(result.attrType,2);
        attributeModule.syncAttribute(result.attrType);
        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.PlatformActivity, eLogMoneyType.PlatformActivityCommonReward);
        PlatformActivityProto.UserPlatformGetRewardRespMsg.Builder respMsg = PlatformActivityProto.UserPlatformGetRewardRespMsg.newBuilder().setRet(0);
        respMsg.setReward(reward);
        player.sendPacket(Protocol.U_PLATFORM_ACTIVITY_GET_REWARD, respMsg);
        return 0;
    }

    public static boolean isOpenType(ePlayerAttrType type){
        ServerInfo serverInfo = ServerListMgr.getServerInfo(GameServer.getInstance().getServerId());
        if(serverInfo == null){
            return false;
        }
        int channelId = serverInfo.getChannelId();
        if(ePlayerAttrType.WanXinBuLuoReward == type){
            if(channelId != eChannelType.MainLand.getValue() && channelId != eChannelType.XiaoChengXuTest.getValue()
                && channelId != eChannelType.MainLandFaXingTest.getValue() && channelId != eChannelType.MainLandBusiness.getValue()){
                return false;
            }
        }

        return ePlayerAttrType.WanXinBuLuoReward == type;
    }

    public static int getActivityChildTypeByType(ePlayerAttrType attrType){
        if(ePlayerAttrType.WanXinBuLuoReward == attrType){
            return ePlatFormChildType.WanXinBuLuo.getIntValue();
        }
        return 0;
    }


    public static int reachCondition(GamePlayer player, int type) {
        SimpleActivityResult check = new SimpleActivityResult();
        check.check(type);
        if(check.ret != 0){
            return check.ret;
        }

        AttributeModule attributeModule = player.getModule(AttributeModule.class);
        long v = attributeModule.getAttribute(check.attrType);
        if(v != 0){
            return GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
        }
        attributeModule.setAttribute(check.attrType,1);
        attributeModule.syncAttribute(check.attrType);

        return 1;
    }

    public static int reachCondition(long userId, int type) {
        SimpleActivityResult check = new SimpleActivityResult();
        check.check(type);
        if(check.ret != 0){
            return check.ret;
        }
        //
        UserInfo userInfo = UserMgr.getUserInfo(userId);
        if(userInfo == null){
            return GameErrorCode.E_USER_NO_FOUND;
        }
        UserAttributeDaoImpl dao = new UserAttributeDaoImpl();
        UserAttribute userAttribute = dao.getUserAttribute(userId, check.attrType);
        if(userAttribute == null){
            userAttribute = new UserAttribute();
            userAttribute.setUserId(userId);
            userAttribute.setType(check.attrType);
            userAttribute.setValue(0);
            userAttribute.setLastUpdateTime(System.currentTimeMillis());
            dao.add(userAttribute);
        }
        if(userAttribute.getValue() != 0){
            return GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
        }
        dao.updateAddUserAttribute(userId, check.attrType, 1);
        return 1;
    }
}
